﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using ZhimeiSoft.Framework.Common.Entity;
using ZhimeiSoft.Framework.Server.Connector.DAL.Interface;
using ZhimeiSoft.Framework.Server.Base;

namespace ZhimeiSoft.Framework.Server.Connector.DAL
{
    public class LoginDal : BaseDal, ILoginDal
    {
        #region ILoginDal Members

        public bool Login(ref LoginEntity loginEntity, out int error)
        {
            error = 0;
            bool bFlag = false;
            try
            {
                if (loginEntity.UserName == string.Empty || loginEntity.Password == string.Empty)
                    return false;

                string sql = "Select UserName, Password from TUsers Where UserName=@UserName";
                DbCommand cmd = DB.GetSqlStringCommand(sql);
                DB.AddInParameter(cmd, "UserName", DbType.String, loginEntity.UserName);
                DataSet ds = DB.ExecuteDataSet(cmd);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow[] drFinds = ds.Tables[0].Select(string.Format("Password='{0}'", loginEntity.Password));
                    if (drFinds.Length > 0)
                    {
                        bFlag = true;
                    }
                    else
                    {
                        error = -2;//密码不正确
                    }
                }
                else
                {
                    error = -1;//用户不存在
                }
            }
            catch (Exception ex)
            {
                error = 10;
            }
            
            return bFlag;
        }

        #endregion
    }
}
